home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / INTPRM.C < prev    next >
C/C++ Source or Header  |  1992-01-20  |  6KB  |  172 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/intprm.c,v 1.3 1989/10/26 07:49:52 cph Rel $
  4.  
  5. Copyright (c) 1989 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Generic Integer Primitives */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include "zones.h"
  40.  
  41. #define INTEGER_TEST(test)                        \
  42. {                                    \
  43.   PRIMITIVE_HEADER (1);                            \
  44.   Set_Time_Zone (Zone_Math);                        \
  45.   CHECK_ARG (1, INTEGER_P);                        \
  46.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (test (ARG_REF (1))));        \
  47. }
  48.  
  49. DEFINE_PRIMITIVE ("INTEGER-ZERO?", Prim_integer_zero_p, 1, 1, 0)
  50.      INTEGER_TEST (integer_zero_p)
  51. DEFINE_PRIMITIVE ("INTEGER-NEGATIVE?", Prim_integer_negative_p, 1, 1, 0)
  52.      INTEGER_TEST (integer_negative_p)
  53. DEFINE_PRIMITIVE ("INTEGER-POSITIVE?", Prim_integer_positive_p, 1, 1, 0)
  54.      INTEGER_TEST (integer_positive_p)
  55.  
  56. #define INTEGER_COMPARISON(comparison)                    \
  57. {                                    \
  58.   PRIMITIVE_HEADER (2);                            \
  59.   Set_Time_Zone (Zone_Math);                        \
  60.   CHECK_ARG (1, INTEGER_P);                        \
  61.   CHECK_ARG (2, INTEGER_P);                        \
  62.   PRIMITIVE_RETURN                            \
  63.     (BOOLEAN_TO_OBJECT (comparison ((ARG_REF (1)), (ARG_REF (2)))));    \
  64. }
  65.  
  66. DEFINE_PRIMITIVE ("INTEGER-EQUAL?", Prim_integer_equal_p, 2, 2, 0)
  67.      INTEGER_COMPARISON (integer_equal_p)
  68. DEFINE_PRIMITIVE ("INTEGER-LESS?", Prim_integer_less_p, 2, 2, 0)
  69.      INTEGER_COMPARISON (integer_less_p)
  70.  
  71. DEFINE_PRIMITIVE ("INTEGER-GREATER?", Prim_integer_greater_p, 2, 2, 0)
  72. {
  73.   PRIMITIVE_HEADER (2);
  74.   Set_Time_Zone (Zone_Math);
  75.   CHECK_ARG (1, INTEGER_P);
  76.   CHECK_ARG (2, INTEGER_P);
  77.   PRIMITIVE_RETURN
  78.     (BOOLEAN_TO_OBJECT (integer_less_p ((ARG_REF (2)), (ARG_REF (1)))));
  79. }
  80.  
  81. #define INTEGER_BINARY_OPERATION(operator)                \
  82. {                                    \
  83.   PRIMITIVE_HEADER (2);                            \
  84.   Set_Time_Zone (Zone_Math);                        \
  85.   CHECK_ARG (1, INTEGER_P);                        \
  86.   CHECK_ARG (2, INTEGER_P);                        \
  87.   PRIMITIVE_RETURN (operator ((ARG_REF (1)), (ARG_REF (2))));        \
  88. }
  89.  
  90. DEFINE_PRIMITIVE ("INTEGER-ADD", Prim_integer_add, 2, 2, 0)
  91.      INTEGER_BINARY_OPERATION (integer_add)
  92. DEFINE_PRIMITIVE ("INTEGER-SUBTRACT", Prim_integer_subtract, 2, 2, 0)
  93.      INTEGER_BINARY_OPERATION (integer_subtract)
  94. DEFINE_PRIMITIVE ("INTEGER-MULTIPLY", Prim_integer_multiply, 2, 2, 0)
  95.      INTEGER_BINARY_OPERATION (integer_multiply)
  96.  
  97. #define INTEGER_UNARY_OPERATION(operator)                \
  98. {                                    \
  99.   PRIMITIVE_HEADER (1);                            \
  100.   Set_Time_Zone (Zone_Math);                        \
  101.   CHECK_ARG (1, INTEGER_P);                        \
  102.   PRIMITIVE_RETURN (operator (ARG_REF (1)));                \
  103. }
  104.  
  105. DEFINE_PRIMITIVE ("INTEGER-NEGATE", Prim_integer_negate, 1, 1, 0)
  106.      INTEGER_UNARY_OPERATION (integer_negate)
  107. DEFINE_PRIMITIVE ("INTEGER-ADD-1", Prim_integer_add_1, 1, 1, 0)
  108.      INTEGER_UNARY_OPERATION (integer_add_1)
  109. DEFINE_PRIMITIVE ("INTEGER-SUBTRACT-1", Prim_integer_subtract_1, 1, 1, 0)
  110.      INTEGER_UNARY_OPERATION (integer_subtract_1)
  111.  
  112. DEFINE_PRIMITIVE ("INTEGER-DIVIDE", Prim_integer_divide, 2, 2, 0)
  113. {
  114.   SCHEME_OBJECT quotient;
  115.   SCHEME_OBJECT remainder;
  116.   PRIMITIVE_HEADER (2);
  117.   Set_Time_Zone (Zone_Math);
  118.   CHECK_ARG (1, INTEGER_P);
  119.   CHECK_ARG (2, INTEGER_P);
  120.   if (integer_divide ((ARG_REF (1)), (ARG_REF (2)), ("ient), (&remainder)))
  121.     error_bad_range_arg (2);
  122.   PRIMITIVE_RETURN (cons (quotient, remainder));
  123. }
  124.  
  125. #define INTEGER_QR(operator)                        \
  126. {                                    \
  127.   SCHEME_OBJECT result;                            \
  128.   PRIMITIVE_HEADER (2);                            \
  129.   Set_Time_Zone (Zone_Math);                        \
  130.   CHECK_ARG (1, INTEGER_P);                        \
  131.   CHECK_ARG (2, INTEGER_P);                        \
  132.   result = (operator ((ARG_REF (1)), (ARG_REF (2))));            \
  133.   if (result == SHARP_F)                        \
  134.     error_bad_range_arg (2);                        \
  135.   PRIMITIVE_RETURN (result);                        \
  136. }
  137.  
  138. DEFINE_PRIMITIVE ("INTEGER-QUOTIENT", Prim_integer_quotient, 2, 2, 0)
  139.      INTEGER_QR (integer_quotient)
  140. DEFINE_PRIMITIVE ("INTEGER-REMAINDER", Prim_integer_remainder, 2, 2, 0)
  141.      INTEGER_QR (integer_remainder)
  142.  
  143. DEFINE_PRIMITIVE ("INTEGER?", Prim_integer_p, 1, 1, 0)
  144. {
  145.   PRIMITIVE_HEADER (1);
  146.   {
  147.     fast SCHEME_OBJECT integer = (ARG_REF (1));
  148.     PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (INTEGER_P (integer)));
  149.   }
  150. }
  151.  
  152. DEFINE_PRIMITIVE ("INTEGER->FLONUM", Prim_integer_to_flonum, 2, 2, 0)
  153. {
  154.   PRIMITIVE_HEADER (2);
  155.   Set_Time_Zone (Zone_Math);
  156.   CHECK_ARG (1, INTEGER_P);
  157.   {
  158.     fast SCHEME_OBJECT integer = (ARG_REF (1));
  159.     fast long control = (arg_index_integer (2, 4));
  160.     if (FIXNUM_P (integer))
  161.       PRIMITIVE_RETURN (FIXNUM_TO_FLONUM (integer));
  162.     if (bignum_fits_in_word_p
  163.     (integer,
  164.      (((control & 1) != 0) ? DBL_MANT_DIG : DBL_MAX_EXP),
  165.      0))
  166.       PRIMITIVE_RETURN (BIGNUM_TO_FLONUM (integer));
  167.     if ((control & 2) != 0)
  168.       error_bad_range_arg (1);
  169.     PRIMITIVE_RETURN (SHARP_F);
  170.   }
  171. }
  172.